package com.testdocs.client.structure.top;

import com.testdocs.client.ClientFactory;
import com.testdocs.client.project.ProjectInfo;
import com.testdocs.client.project.create.CreateProjectPlace;
import com.testdocs.client.project.dashboard.DashboardPlace;
import com.testdocs.client.project.event.ProjectCreatedEvent;
import com.testdocs.client.project.event.ProjectCreatedEventHandler;
import com.testdocs.client.project.event.ProjectDeselectedEvent;
import com.testdocs.client.project.event.ProjectDeselectedEventHandler;
import com.testdocs.client.project.event.ProjectRemovedEvent;
import com.testdocs.client.project.event.ProjectRemovedEventHandler;
import com.testdocs.client.project.event.ProjectSelectedEvent;
import com.testdocs.client.project.event.ProjectSelectedEventHandler;
import com.testdocs.client.project.event.ProjectsListLoadedEvent;
import com.testdocs.client.project.event.ProjectsListLoadedEventHandler;
import com.testdocs.client.signin.event.SignInEvent;
import com.testdocs.client.signin.event.SignInEventHandler;
import com.testdocs.client.signin.event.SignOutEvent;
import com.testdocs.client.signin.event.SignOutEventHandler;

public class TopPanelPresenter implements TopPanel.Presenter {
	private ClientFactory clientFactory;
	
	public TopPanelPresenter(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		clientFactory.getEventBus().addHandler(ProjectSelectedEvent.TYPE, new ProjectSelectedHandler());
		clientFactory.getEventBus().addHandler(ProjectDeselectedEvent.TYPE, new ProjectDeselectedHandler());
		clientFactory.getEventBus().addHandler(ProjectsListLoadedEvent.TYPE, new ProjectsListLoadedHandler());
		clientFactory.getEventBus().addHandler(ProjectCreatedEvent.TYPE, new ProjectCreatedHandler());
		clientFactory.getEventBus().addHandler(ProjectRemovedEvent.TYPE, new ProjectRemovedHandler());
		clientFactory.getEventBus().addHandler(SignInEvent.TYPE, new SignInHandler());
		clientFactory.getEventBus().addHandler(SignOutEvent.TYPE, new SignOutHandler());
		
	}
	
	public class ProjectSelectedHandler implements ProjectSelectedEventHandler {
		@Override
		public void projectSelected(ProjectSelectedEvent event) {
			clientFactory.getTopPanel().setProjectInfo(event.getProjectInfo());
		}
	}
	
	public class ProjectDeselectedHandler implements ProjectDeselectedEventHandler {
		@Override
		public void projectDeselected(ProjectDeselectedEvent event) {
			clientFactory.getTopPanel().setNoProjectSelected();
		}
	}
	
	public class SignInHandler implements SignInEventHandler {
		@Override
		public void signedIn(SignInEvent signInEvent) {
			clientFactory.getTopPanel().setSignedInView();
			clientFactory.getTopPanel().setSignOutUrl(signInEvent.getLoginInfo().getSignOutUrl());
			clientFactory.getTopPanel().setUser(signInEvent.getLoginInfo().getNickname());
		}
	}
	
	public class SignOutHandler implements SignOutEventHandler {
		public void signedOut(SignOutEvent signOutEvent) {
			clientFactory.getTopPanel().setSignedOutView();
		}
	}
	
	public class ProjectsListLoadedHandler implements ProjectsListLoadedEventHandler {
		@Override
		public void projectsListLoaded(ProjectsListLoadedEvent event) {
			clientFactory.getTopPanel().setProjectsList(event.getProjects());
		}
	}

	public class ProjectCreatedHandler implements ProjectCreatedEventHandler {
		@Override
		public void projectCreated(ProjectCreatedEvent event) {
			clientFactory.getTopPanel().addProjectToList(event.getProject());
		}
	}
	
	public class ProjectRemovedHandler implements ProjectRemovedEventHandler {
		public void projectRemoved(ProjectRemovedEvent event) {
			clientFactory.getTopPanel().removeProjectFromList(event.getProject());			
		}
	}

	@Override
	public void onSignOut() {
		clientFactory.getEventBus().fireEvent(new ProjectDeselectedEvent());
		clientFactory.getEventBus().fireEvent(new SignOutEvent());
	}

	@Override
	public void onProjectSelect(ProjectInfo projectInfo) {
		clientFactory.getEventBus().fireEvent(new ProjectSelectedEvent(projectInfo));
		clientFactory.getPlaceController().goTo(new DashboardPlace(projectInfo.getProjectId()));
	}
	
	@Override
	public void onCreateNewProject() {
		clientFactory.getPlaceController().goTo(new CreateProjectPlace());
	}
}
